{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{:lang('hostuser')}{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="{:lang('pleaseEnter')}关键字">
        </div>
        入读时间范围：
        <div class="layui-inline">
            <input class="layui-input startDate" name="key" id="dateRange" placeholder="-">
        </div>

        <button class="layui-btn" id="search" data-type="reload">搜索</button>
        <a href="{:url('index')}" class="layui-btn">显示全部</a>

    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="user_sex">
    <input type="checkbox" name="user_sex" value="{{d.host_user_id}}" lay-skin="switch" lay-text="男|女" lay-filter="user_sex" {{ d.user_sex == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
    <a href="{:url('edit')}?id={{d.host_user_id}}" class="layui-btn layui-btn-xs">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="email">
    {{d.email}}
    {{# if(d.email && d.email_validated=='0'){ }}
    (未验证)
    {{# } }}
</script>
<script type="text/html" id="topBtn">
    <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" id="delAll">批量删除</button>
<!--    <button type="button" class="layui-btn  layui-btn-sm" id="create">-->
       <a href="{:url('addUser')}" class="layui-btn  layui-btn-sm" >添加客户</a>
<!--    </button>-->
</script>
<script type="text/html" id="sex">
    {{# if(d.sex=='0'){ }}
    女
    {{# }else{ }}
    男
    {{# } }}
</script>
{include file="common/foot"/}
<script>
    layui.use('laydate', function() {
        var laydate = layui.laydate;
        //日期范围
        laydate.render({
            elem: '#dateRange'
            ,range: true
        });

    });

    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var exportData = '';
        var tableIn = table.render({
            id: 'user',
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            toolbar: '#topBtn',
            totalRow: true,
            page: true,
            cols: [[
                {checkbox:true,fixed: true},
                {field: 'host_user_id', title: '{:lang("id")}', width: 60, fixed: true},
                {field: 'student_name', title: '姓名', width: 100},
                {field: 'user_sex', align: 'center',title: '{:lang("status")}', width: 120,toolbar: '#user_sex'},
                {field: 'birth_date', title: '生日', width: 100},
                {field: 'school_name', title: '学校', width: 100},
                {field: 'school_address', title: '学校地址', width: 100,edit: 'text'},
                {field: 'grade_Enrolled', title: '入读年级', width: 100},
                {field: 'the_date', title: '入读时间', width: 100},
                {field: 'Check_deadlines', title: '入读期限', width: 100},
                {field: 'level_name', title: '其他', width: 60},
                {field: 'host_tyle', title: '类型需求', width: 100},
                {field: 'add_time', title: '提交时间', width: 100},
                {width: 160, align: 'center', toolbar: '#action'}
            ]],
            limit: 10,//每页默认显示的数量
             done: function (res, curr, count) {
                exportData=res.data;        //获取表格中的数据集合。
            }

        });
        //====================

        table.on('toolbar(list)', function(obj){
            switch(obj.event){
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_EXPORT':
             
                    var checkStatus = table.checkStatus('user'); //test即为参数id设定的值
                    var data = checkStatus.data;
                    if(data.length == 0){
                        data = exportData;
                    }
                    var cols = obj.config.cols[0];
                    var col = [];
                    for(i=1;i<cols.length-1;i++){
                        // if( cols[i].hide == false){
                            col.push(cols[i].title) ;
                        // }
                    }
                    col.push("Ip");
                    table.exportFile(col,data, 'csv');

                    break;
            };
        })
        //=========================
//监听单元格编辑
        table.on('edit(list)', function(obj){
            var datainfo = obj.data;
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段

            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("changeAddr")}',{'id':datainfo.host_user_id,'school_address':value},function (res) {
                console.log(res);
                layer.close(loading);
                if (res.status==1) {
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });

        form.on('switch(user_sex)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var user_sex = obj.elem.checked===true? 1:2;
            $.post('{:url("changeState")}',{'id':id,'user_sex':user_sex},function (res) {
                layer.close(loading);
                if (res.status==1) {
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        //搜索
        $('#search').on('click', function() {
            var key = $('#key').val();
            var dateRange = $("#dateRange").val();
            if($.trim(key)==='' && $.trim(dateRange)=== '' ){
                layer.msg('{:lang("pleaseEnter")}关键字或请选择时间段！',{icon:0});
                return;
            }

            tableIn.reload({ page: {page: 1},where: {key: key,dateRange:dateRange}});
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('您确定要删除该会员吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('usersDel')}",{id:data.host_user_id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('body').on('click','#delAll',function() {
            layer.confirm('确认要删除选中信息吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('user'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.host_user_id);
                });

                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delall')}", {ids: ids}, function (data) {
                    console.log(data);
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        });

    });
</script>
</body>
</html>